package controller

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/app/web/service/report"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Report struct{}

// 新增编辑报告配置
func (r Report) SaveConfig(c *gin.Context) {
	var params struct {
		ID              string `json:"id"`
		PutType         int    `json:"put_type"` // 推送类型 1运营日报 2单日空货道 3连续空货道 4电量报告 5空货周报
		ObjectName      string `json:"object_name"`
		RobotSelectType int    `json:"robot_select_type"` // 1全部 2指定机器 3指定商户
		MerchantSelect  string `json:"merchant_select"`   // 指定商户，逗号分隔
		RobotName       string `json:"robot_name"`
		WebHookName     int    `json:"web_hook_name"`
		WebHookAddress  string `json:"web_hook_address"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := report.ReportConfigService{}.SaveConfig(c.Request.Context(), db,
		c.GetString("merchant_id"),
		params.ID,
		params.PutType,
		params.RobotSelectType,
		params.MerchantSelect,
		params.RobotName,
		params.WebHookName,
		params.WebHookAddress,
		params.ObjectName,
	)
	response.JSON(c, nil, err)
}

// 获取推送方式
func (r Report) GetReportConfig(c *gin.Context) {
	data := map[int]interface{}{
		1: "飞书",
		2: "邮箱",
	}
	response.JSON(c, data, nil)
}

// 删除报告配置
func (r Report) DelConfig(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := report.ReportConfigService{}.DelConfig(c.Request.Context(), db, params.ID)
	response.JSON(c, nil, err)
}

// 运营报告配置列表
func (r Report) ConfigList(c *gin.Context) {
	var params struct {
		PutType int `json:"put_type"`
		Page    int `json:"page"`
		Limit   int `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	list, count, err := report.ReportConfigService{}.ConfigList(
		c.Request.Context(), db, merchantID, params.PutType,
		params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"count": count,
	}, err)
}

// 事件类型
func (r Report) AddEventType(c *gin.Context) {
	var params struct {
		Name string `json:"name"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := report.ReportTypeService{}.AddEventType(c.Request.Context(), db, params.Name)
	response.JSON(c, nil, err)
}

func (r Report) DelEventType(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := report.ReportTypeService{}.DelEventType(c.Request.Context(), db, params.ID)
	response.JSON(c, nil, err)
}

func (r Report) EventTypeList(c *gin.Context) {
	var params struct {
		Page  int `json:"page"`
		Limit int `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	list, count, err := report.ReportTypeService{}.EventTypeList(c.Request.Context(), db, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"count": count,
	}, err)
}

func (r Report) AddEventQuestion(c *gin.Context) {
	var params struct {
		EventType string `json:"event_type"`
		Content   string `json:"content"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := report.EventQuestionService{}.AddEventQuestion(c.Request.Context(), db, params.EventType, params.Content)
	response.JSON(c, nil, err)
}

func (r Report) EditEventQuestion(c *gin.Context) {
	var params struct {
		ID        string `json:"id"`
		EventType string `json:"event_type"`
		Content   string `json:"content"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := report.EventQuestionService{}.EditEventQuestion(c.Request.Context(), db, params.ID, params.EventType, params.Content)
	response.JSON(c, nil, err)
}

func (r Report) DelEventQuestion(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := report.EventQuestionService{}.DelEventQuestion(c.Request.Context(), db, params.ID)
	response.JSON(c, nil, err)
}

func (r Report) EventQuestionList(c *gin.Context) {
	var params struct {
		Page  int `json:"page"`
		Limit int `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	list, count, err := report.EventQuestionService{}.EventQuestionList(c.Request.Context(), db, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"count": count,
	}, err)
}

// 添加运营事件
func (r Report) AddEvent(c *gin.Context) {
	var params struct {
		SalesID      string `json:"sales_id"`
		RobotName    string `json:"robot_name"`
		Date         string `json:"date"`
		Event        string `json:"event"`      //事件问题类型
		EventType    string `json:"event_type"` //事件类型
		EeventDesc   string `json:"eevent_desc"`
		AffectedTime string `json:"affected_time"`
		Location     string `json:"location"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	userID := c.GetString("user_id")
	err := report.ReportEventService{}.AddEvent(c.Request.Context(), db, merchantID, params.SalesID, params.RobotName, params.Date, params.Event,
		params.EventType, params.EeventDesc, params.AffectedTime, params.Location, userID)
	response.JSON(c, nil, err)
}

// 编辑运营事件
func (r Report) EditEvent(c *gin.Context) {
	var params struct {
		ID           int    `json:"id"` // 编辑的ID
		SalesID      string `json:"sales_id"`
		RobotName    string `json:"robot_name"`
		Date         string `json:"date"`
		Event        string `json:"event"`      //事件问题类型
		EventType    string `json:"event_type"` //事件类型
		EeventDesc   string `json:"eevent_desc"`
		AffectedTime string `json:"affected_time"`
		Location     string `json:"location"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	userID := c.GetString("user_id")
	err := report.ReportEventService{}.EditEvent(c.Request.Context(), db, merchantID, params.SalesID, params.RobotName, params.Date, params.Event,
		params.EventType, params.EeventDesc, params.AffectedTime, params.Location, userID, params.ID)
	response.JSON(c, nil, err)
}

// 删除运营事件
func (r Report) DelEvent(c *gin.Context) {
	var params struct {
		ID int `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := report.ReportEventService{}.DelEvent(c.Request.Context(), db, params.ID)
	response.JSON(c, nil, err)
}

func (r Report) EventDesc(c *gin.Context) {
	var params struct {
		SalesID   string    `json:"sales_id"`
		StartTime string    `json:"start_time"`
		EndTime   string    `json:"end_time"`
		EventType string    `json:"event_type"`
		ShopID    *[]string `json:"shop_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	var shopIDList = []string{}
	if params.ShopID != nil {
		shopIDList = append(shopIDList, *params.ShopID...)
	}

	data, top, count, err := report.ReportEventService{}.EventDesc(c.Request.Context(), db, merchantID, params.SalesID, params.StartTime, params.EndTime, params.EventType, shopIDList)
	response.JSON(c, gin.H{
		"list":  data,
		"top":   top,
		"count": count,
	}, err)
}

func (r Report) EventList(c *gin.Context) {
	var params struct {
		Page  int `json:"page"`
		Limit int `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	list, count, err := report.ReportEventService{}.EventList(c.Request.Context(), db, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"count": count,
	}, err)
}

// 获取报告列表
func (r Report) RobotReportLog(c *gin.Context) {
	var params struct {
		Type      int    `json:"type"`
		StartTime string `json:"start_time"`
		EndTime   string `json:"end_time"`
		Page      int    `json:"page"`
		Limit     int    `json:"limit"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	list, count, err := report.ReportLogService{}.RobotReportLog(c.Request.Context(), db,
		merchantID, params.Type, params.StartTime, params.EndTime, params.Page, params.Limit)

	response.JSON(c, gin.H{
		"list":  list,
		"count": count,
	}, err)
}

// 报告详情
func (r Report) LogDetail(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	db := mysql.NewDB()

	// 获取新的报告详情
	data, titleInfo, err := report.ReportService{}.LogDetail(c.Request.Context(), db, merchantID, params.ID)

	response.JSON(c, gin.H{
		"data":       data,      // excel数据
		"title_info": titleInfo, // 标题信息。日期，推送选择，推送地址
	}, err)
}
